package com.mdd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.BaseContext;
import com.mdd.common.R;
import com.mdd.dto.OrderDto;
import com.mdd.pojo.OrderDetail;
import com.mdd.pojo.Orders;
import com.mdd.service.OrderDetailService;
import com.mdd.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    @Transactional
    public R<String>  again(@RequestBody Orders order){
        //获取原订单信息
        Orders orders = orderService.getById(order.getId());
        //设置新订单
        Orders newOrders = new Orders();
        BeanUtils.copyProperties(orders,newOrders);
        long id = IdWorker.getId();
        newOrders.setId(id);
        newOrders.setNumber(String.valueOf(id));
        newOrders.setOrderTime(LocalDateTime.now());
        newOrders.setCheckoutTime(LocalDateTime.now());
        //添加
        orderService.save(newOrders);
        //获取原订单明细
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,order.getId());
        List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);

        //修改订单明细的订单号位新订单的订单号
        for (OrderDetail orderDetail : orderDetails) {
            orderDetail.setId(null);
            orderDetail.setOrderId(id);
        }
        //批量插入明细表
        orderDetailService.saveBatch(orderDetails);
        return R.success("下单成功！");
    }

    /**x``
     * 账单提交
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("账单提交成功");
    }

    /**
     * 显示当前用户的所有订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrderDto>> orderPage(int page, int pageSize){
        Page<OrderDto> orderDtoPage = orderService.showUserOrder(page, pageSize,BaseContext.getCurrentId(),null,null,null);
        return R.success(orderDtoPage);
    }


    /**
     * 条件分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrderDto>> backPage(int page,int pageSize,Long number,String beginTime,String endTime){
        Page<OrderDto> orderDtoPage = orderService.showUserOrder(page, pageSize, null,number,beginTime,endTime);
        return R.success(orderDtoPage);
    }
}
